package cn.nubia.admin.sysconfig.service;

import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.sysconfig.bean.DictField;
import cn.nubia.admin.sysconfig.bean.DictFieldItem;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.query.DictFieldQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2015/7/8.
 */
@Service
public class DictFieldService extends BaseServiceImpl<DictField>{

    @Autowired
    SystemLogService systemLogService;

    /**
     *
     * @param dictFieldQuery
     * @return
     */
    public Paged<DictField> list(DictFieldQuery dictFieldQuery) {
        Query query = Query.build(DictField.class);
        query.addEq("deleted",0);
        query.addLike("fieldName",dictFieldQuery.getFieldName());
        query.addLike("fieldTitle",dictFieldQuery.getFieldTitle());
        query.addOrder("id", Query.DBOrder.DESC);
        query.setPaged(dictFieldQuery.getPageNo(),dictFieldQuery.getPageSize());
        return findPagedByQuery(query);
    }

    public void save(DictField dictField) {
        Integer currTime = DateUtil.getCurrentTime();
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        dictField.setLastModifyTime(currTime);
        SystemLog.OperateTypeEnum operateTypeEnum = SystemLog.OperateTypeEnum.ADD;

        if(dictField.getId()==null) {
            dictField.setCreateAdminId(WebContext.getCachedUserId());
            dictField.setCreateAdminName(WebContext.getCachedUser().getName());
            dictField.setCreateTime(DateUtil.getCurrentTime());
            operateTypeEnum = SystemLog.OperateTypeEnum.ADD;

        }else{
            DictField domain = get(dictField.getId());
            dictField.setCreateAdminId(domain.getCreateAdminId());
            dictField.setCreateTime(domain.getCreateTime());
            dictField.setCreateAdminName(domain.getCreateAdminName());

            dictField.setModifyAdminId(WebContext.getCachedUserId());
            dictField.setModifyAdminName(WebContext.getCachedUser().getName());
            operateTypeEnum = SystemLog.OperateTypeEnum.EDIT;

        }
        this.getDAO().save(dictField);
        systemLogService.log(SystemLog.ModuleTypeEnum.DICTFIELDLIST, operateTypeEnum, dictField.getId());

    }


    public DictField view(Integer id) {
        return get(id);
    }

    @Override
    public void delete(int id) {
        DictField dictField = new DictField();
        dictField.setId(id);
        dictField.setDeleted(1);
       update(dictField);
    }

    public List<DictField> getDictFieldList() {
        Query query = Query.build(DictField.class);
        query.addEq("deleted", 0);
        return findByQuery(query);
    }

    public boolean check(Integer id, String fieldName) {
        Query query = Query.build(DictField.class);
        query.addEq("fieldName",fieldName);
        query.addEq("deleted",0);
        return isExist(query, id);
    }

    public DictField getDictFieldByName(String fieldName) {
        Query query = Query.build(DictField.class);
        query.addEq(DictField.COLUMN_FIELDNAME,fieldName);
        query.addOrder(DictField.COLUMN_CREATETIME, Query.DBOrder.DESC);
        return get(query);
    }
}
